package model;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
	private List<ProductModel> products;
	private float total;
	
	public ShoppingCart(){
		this.products = new ArrayList<ProductModel>();
		this.total = 0;
	}
	
	public List<ProductModel> getProducts() {
		return products;
	}
	public void setProducts(List<ProductModel> products) {
		this.products = products;
	}
	public float getTotal() {
		return total;
	}
	public void setTotal(float total) {
		this.total = total;
	}
	
	/*
	 * Tinh tong so tien trong gio hang
	 */
	
	public float calTotal(){
		float total=0;
		for(ProductModel p: products){
			total += p.getPrice()* p.getQuantities();
		}
		return total;
	}
	
	public void add(String id,int s,ProductModel h){
		int flag=0;
		for(int i=0;i<this.products.size();i++){
			if(products.get(i).getId()== id){
				products.get(i).IncreaseQty(s);
				flag=1;
				break;
			}
			
			
		}
		if(flag==0){
			this.products.add(h);
		}
		
		/*if(this.giohang.contains(h)){
			for(int i=0;i<this.giohang.size();i++){
				Hoa hTemp = this.giohang.get(i);
				if(hTemp.equals(h)){
					hTemp.tangSoluong(h.getSoluong());
					this.giohang.set(i, hTemp);
					break;					
				}		
			}	
			
		}
		else {			
			this.giohang.add(h);	
			
		}*/
	}
	
	public void delete(ProductModel h){
		//if(this.giohang.contains(h)){
		//	this.giohang.remove(h);
		//}
		for(int i=0;i<products.size();i++){
			if(products.get(i).getId()==h.getId()){
				products.remove(products.get(i));
			}
			
		}
	}
	public String toString(){
		return this.products.toString();
		
	}
	public String printOut(){
		for(ProductModel h : products){
			return h.getName();
			
		}
		return null;
	}
	
	public void updateQty(ProductModel h, int n){
		for(int i = 0;i<this.products.size();i++){
			if(products.get(i).getId()==h.getId())	{
				products.get(i).setQuantities(n);
			}	
		}
		
	}
}
